import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
//465
class Student {
    private String name;
    private String studentId;
    private String cours;
    private double score;
    private final String course;

    public Student(String name, String studentId, String course, double score) {
        this.name = name;
        this.studentId = studentId;
        this.course = course;
        this.score = score;
    }

    // Getter方法
    public String getName() { return name; }
    public String getStudentId() { return studentId; }
    public String getCourse() { return course; }
    public double getScore() { return score; }
}

public class Demo {
    private static final List<Student> students = new ArrayList<>();
    private static final Scanner scanner = new Scanner(System.in);

    // 记录学生成绩 
    private static void recordGrade() {
        System.out.println("===== 记录学生成绩 =====");
        System.out.print("请输入学生姓名：");
        String name = scanner.nextLine();
        
        System.out.print("请输入学生学号：");
        String studentId = scanner.nextLine();
        // 检测重复学号
        if (students.stream().anyMatch(s -> s.getStudentId().equals(studentId))) {
            System.out.println("错误：该学号已存在！");
            return;
        }
        
        System.out.print("请输入课程名称：");
        String course = scanner.nextLine();
        
        System.out.print("请输入成绩（0-100）：");
        double score = scanner.nextDouble();
        if (score < 0 || score > 100) {
            System.out.println("错误：成绩必须在0-100之间！");
            return;
        }
        scanner.nextLine(); // 消耗换行符
        
        students.add(new Student(name, studentId, course, score));
        System.out.println("成绩已成功记录！");
    }

    // 查询成绩
    private static void queryGrade() {
        System.out.println("===== 查询学生成绩 =====");
        System.out.println("请选择查询方式：\n1. 按学生姓名查询\n2. 按学生学号查询\n3. 按课程名称查询");
        int choice = scanner.nextInt();
        scanner.nextLine(); // 消耗换行符
        
        System.out.print("请输入查询关键词：");
        String keyword = scanner.nextLine();
        List<Student> results = new ArrayList<>();

        switch (choice) {
            case 1:
                results = students.stream().filter(s -> s.getName().equals(keyword)).toList();
                break;
            case 2:
                results = students.stream().filter(s -> s.getStudentId().equals(keyword)).toList();
                break;
            case 3:
                results = students.stream().filter(s -> s.getCourse().equals(keyword)).toList();
                break;
            default:
                System.out.println("无效选项！");
                return;
        }

        if (results.isEmpty()) {
            System.out.println("未找到相关记录！");
        } else {
            results.forEach(s -> System.out.printf("姓名：%s, 学号：%s, 课程：%s, 成绩：%.1f%n",
                    s.getName(), s.getStudentId(), s.getCourse(), s.getScore()));
        }
    }

 // 统计成绩
private static void statisticsGrade() {
    System.out.println("===== 统计课程成绩 =====");
    System.out.print("请输入课程名称：");
    String course = scanner.nextLine();
    
    // 筛选该课程的所有成绩
    List<Student> courseStudents = students.stream()
            .filter(s -> s.getCourse().equals(course))
            .toList();
    
    if (courseStudents.isEmpty()) {
        System.out.println("该课程无成绩记录！");
        return;
    }
    
    // 提取成绩列表并计算统计值
    double sum = 0.0;
    double max = Double.MIN_VALUE;
    double min = Double.MAX_VALUE;
    
    for (Student s : courseStudents) {
        double score = s.getScore();
        sum += score;
        if (score > max) max = score;
        if (score < min) min = score;
    }
    
    double avg = sum / courseStudents.size();
    
    System.out.printf("课程：%s%n平均分：%.2f%n最高分：%.1f%n最低分：%.1f%n",
            course, avg, max, min);
}

    public static void main(String[] args) {
        while (true) {
            System.out.println("\n=================================");
            System.out.println("欢迎使用学生成绩管理系统");
            System.out.println("=================================");
            System.out.println("请选择操作：\n1. 记录学生成绩\n2. 查询学生成绩\n3. 统计课程成绩\n4. 退出系统");
            System.out.print("请输入选项序号：");
            
            int option = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符
            
            switch (option) {
                case 1:
                    recordGrade();
                    break;
                case 2:
                    queryGrade();
                    break;
                case 3:
                    statisticsGrade();
                    break;
                case 4:
                    System.out.println("感谢使用学生成绩管理系统，再见！");
                    scanner.close();
                    return;
                default:
                    System.out.println("无效选项，请重新输入！");
            }
        }
    }
}